__author__ = 'zm'
import sys
sys.path.append("..")

from CoreSim.FlowScheduler import *
from CoreSim.Flow import *

inDir = "Input/"
outDir = "Output/"

class TestFlowScheduler(FlowScheduler):
    def AssignFlows(self, args):
        """
        args (type, filename)
        """
        self.type = args[0]
        self.fileName = args[1]
        f = open(inDir + self.fileName, "r")
        for line in f.readlines():
            l = line.split()
            print l
            flow = Flow()
            if self.type == 'CDF':
                flow.startId = int(l[1])
                flow.endId = int(l[2])
                flow.SetFlowSize(float(l[4])*8)
                flow.startTime = float(l[0])
                flow.flowId = len(self.flows) + 1
            else:
                flow.startId = int(l[0])
                flow.endId = int(l[1])
                flow.flowId = len(self.flows) + 1
            self.flows.append(flow)
        f.close()

    def PrintFlows(self):
        f_name = outDir + "K%d_S%0.0f_L%d_a%0.1f_out.txt" \
                 % (self.K, self.flowSize, self.mean, self.alpha)
        f = open(f_name, "w")
        f_name = outDir + "K%d_S%0.0f_L%d_a%0.1f_plot.dat" \
                 % (self.K, self.flowSize, self.mean, self.alpha)
        f_plot = open(f_name, "w")
        for flow in self.finishedFlows:
            flowTransTime = flow.finishTime - flow.startTime
            print >> f, "flow %d used %f\t%f\t%f" % (flow.flowId, flowTransTime, flow.startTime, flow.finishTime)
            flow.bw = flow.flowSize / flowTransTime
        # print bandwidth (in Mbps) in each line with sorted format
        bwList = [flow.bw for flow in self.finishedFlows]
        bwList.sort()
        num = len(bwList)
        for i in range(num):
            print >> f_plot, "%f\t%f" % (bwList[i] / Mb, float(i + 1) / num)
        f.close()
        f_plot.close()
